package com.bigstar.lolen_e;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.Toast;

public class ChampionCard extends ImageView {

	private boolean back;
	private boolean action;
	private boolean turn;
	private Context context;
	public boolean set;
	public String name;
	private Rect r;

	public ChampionCard(Context context) {
		super(context);
		this.context = context;
		action = false;
		back = true;
		turn = false;
		set = false;
	}

	public ChampionCard(Context context, AttributeSet attrs) {

		super(context, attrs);
		this.context = context;
		action = false;
		back = true;
		turn = false;
		set = false;
	}

	public ChampionCard(Context context, AttributeSet attrs, int defStyle) {

		super(context, attrs, defStyle);
		this.context = context;
		action = false;
		back = true;
		turn = false;
		set = false;
	}
	
	public void init(){
		r = new Rect(0, 0, this.getLayoutParams().width, this.getLayoutParams().height);
		action = false;
		back = true;
		turn = false;
		set = false;
	}

	public void action() {
		if (!action && !set) {
			action = true;
			if(turn){
				turn = false;	
			}else{
				turn = true;	
			}
			this.invalidate();
		}
	}

	@Override
	public void onDraw(Canvas canvas) {
		if (action) {
			if(turn){
				if (back) {
					if (r.left < this.getLayoutParams().width/2) {
						r.set(r.left + 5, r.top, r.right - 5, r.bottom);
						canvas.drawBitmap(CardTurn.img.get("back"), null, r, null);
						this.invalidate();
					} else {
						r.set(this.getLayoutParams().width/2, r.top, this.getLayoutParams().width/2, r.bottom);
						canvas.drawBitmap(CardTurn.img.get("back"), null, r, null);
						this.invalidate();
						back = false;
					}
				} else {
					if (r.left > 0) {
						r.set(r.left - 5, r.top, r.right + 5, r.bottom);
						canvas.drawBitmap(CardTurn.img.get(name), null, r, null);
						this.invalidate();
					} else {
						r.set(0, 0, this.getLayoutParams().width, this.getLayoutParams().height);
						canvas.drawBitmap(CardTurn.img.get(name), null, r, null);
						action = false;
						if(CardTurn.start){
							for(int i = 0; i < CardTurn.CARD_COUNT; i++){
								if(R.id.card_00 + i == this.getId()){
									continue;
								}
								ChampionCard card = (ChampionCard) ((Activity)context).findViewById(R.id.card_00 + i);
								if(card.isTurn()){
									if(name.equals(card.getName())){
										this.set = true;
										card.set = true;
										int setCount = 0;
										for(int j = 0; j < CardTurn.CARD_COUNT; j++){
											ChampionCard setCard = (ChampionCard) ((Activity)context).findViewById(R.id.card_00 + j);
											if(setCard.set){
												setCount++;
											}
										}	
										if(setCount >= CardTurn.CARD_COUNT){
											SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
											int sec = (int) (pref.getLong("card_time", 0)/1000);
											int mil_sec = (int) (pref.getLong("card_time", 0)%1000);
											int cur_sec = (int) (((CardTurn)context).gameTime/1000);
											int cur_mil_sec = (int) (((CardTurn)context).gameTime%1000);
											if(pref.getLong("card_time", 0) <= ((CardTurn)context).gameTime && pref.getLong("card_time", 0) != 0){
												Toast.makeText(context, "best time : "+ sec+"."+mil_sec+"sec, current time : "+ cur_sec+"."+cur_mil_sec+"sec", Toast.LENGTH_SHORT).show();	
											}else{
												SharedPreferences.Editor editor = pref.edit();
												editor.putLong("card_time", ((CardTurn)context).gameTime);
												editor.commit();
												Toast.makeText(context, "best time update : "+ cur_sec+"."+cur_mil_sec+"sec, before best time : "+ sec+"."+mil_sec+"sec", Toast.LENGTH_SHORT).show();													
											}
											((CardTurn)context).finish();
										}
										return;
									}	
								}
							}
							int turnCount = 0;
							for(int i = 0; i < CardTurn.CARD_COUNT; i++){
								ChampionCard card = (ChampionCard) ((Activity)context).findViewById(R.id.card_00 + i);
								if(card.isTurn() && !card.set){
									turnCount++;
								}
							}	
							if(turnCount > 1){
								for(int i = 0; i < CardTurn.CARD_COUNT; i++){
									ChampionCard card = (ChampionCard) ((Activity)context).findViewById(R.id.card_00 + i);
									if(card.isTurn()){
										card.action();
									}
								}	
							}
						}
					}
				}	
			}else{
				if (back) {
					if (r.left > 0) {
						r.set(r.left - 5, r.top, r.right + 5, r.bottom);
						canvas.drawBitmap(CardTurn.img.get("back"), null, r, null);
						this.invalidate();
					} else {
						r.set(0, 0, this.getLayoutParams().width, this.getLayoutParams().height);
						canvas.drawBitmap(CardTurn.img.get("back"), null, r, null);
						action = false;
					}
				} else {
					if (r.left < this.getLayoutParams().width/2) {
						r.set(r.left + 5, r.top, r.right - 5, r.bottom);
						canvas.drawBitmap(CardTurn.img.get(name), null, r, null);
						this.invalidate();
					} else {
						r.set(this.getLayoutParams().width/2, r.top, this.getLayoutParams().width/2, r.bottom);
						canvas.drawBitmap(CardTurn.img.get(name), null, r, null);
						this.invalidate();
						back = true;
					}
				}
			}
		}else{
			if(back)
				canvas.drawBitmap(CardTurn.img.get("back"), null, r, null);
			else
				canvas.drawBitmap(CardTurn.img.get(name), null, r, null);
		}

		super.onDraw(canvas);
	}
	
	public boolean isAction(){
		return action;
	}
	
	public boolean isBack(){
		return back;
	}
	
	public boolean isTurn(){
		return turn;
	}
	
	public boolean isSet(){
		return set;
	}
	
	public String getName(){
		return name;
	}
	public void setName(String name){
		this.name = name;
	}
	
	public void setHeight(int height){
		this.getLayoutParams().height = height;
	}
	
	public void setWidth(int width){
		this.getLayoutParams().width = width;
	}
}
